<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/6/16
 * Time: 21:40
 */

namespace common\models\Service;


use common\models\CommonFunction;
use common\models\CommonVar;
use common\models\DAO\RegionPhone;
use common\models\DAO\User;
use common\models\DAO\UserCode;
use common\models\Http;
use common\msg\UserMsg;

class RegService {
    public static function app_set($data){
        $userModel = new User();
        $code = $data['code'];
        $tel = CommonFunction::encrypt($data['tel']);
        $user =  $userModel->findOne(array('phone'=>$tel));
        $error['status'] = 0;
        $error['msg'] = UserMsg::$time_out;
        if($user){
            $error['msg'] = UserMsg::$reg_already;
            return $error;
        }
        $today = time();
        $now = $today - 60;
        //array('condition' => 'tel=:state', 'params' => array(':state'=>$tel))
        $userCode = UserCode::find()->where(array('tel'=>$tel,  'type'=>1))->andFilterWhere(['>', 'createTime', $now])->orderBy('createTime desc')->asArray()->one();
        if(!$userCode){
            $error['msg'] = UserMsg::$send_error;
            return $error;
        }
        if($userCode['code'] != $code){
            $error['msg'] = UserMsg::$code_error;
            return $error;
        }
        $encrypt = CommonFunction::getRandChar(6);
        $userModel->encrypt = $encrypt;
        $password = CommonFunction::encryptPassword($data['password'], $encrypt);
        $userModel->loginPassword = $password;
        $userModel->phone = $tel;
        $userModel->createTime = $today;
        $userModel->updateTime = $today;
        $return = array('status'=>1);
        if(!empty($data['rec']) && $data['rec'] != $data['tel']){
            $rec_tel = CommonFunction::encrypt($data['rec']);
            $rec_info = $userModel->findOne(array('phone'=>$rec_tel));
            if($rec_info){
                $rec_info = $rec_info->attributes;
                $userModel->recID = $rec_info['id'];
            }
        }
        $key = md5($tel.CommonVar::$encrypt.$password.$today);
        $userModel->appKey = $key;
        $return['app_key'] = $key;
        $region_info = RegionService::get($data['tel']);
        $userModel->city = $region_info['c'];
        $userModel->province = $region_info['p'];
        $userModel->place = $data['typeID'];
        isset($data['token']) && $userModel->token = $data['token'];
        $userModel->appKey = $key;
        $result = $userModel->save();
        if(!$result)
            return $error;

        return $return;
    }


    public static function set($data, $app = false){
        $userModel = new User();
        $code = $data['code'];
        $tel = CommonFunction::encrypt($data['tel']);
        $user =  $userModel->findOne(array('phone'=>$tel));
        $error['status'] = 0;
        $error['msg'] = UserMsg::$time_out;
        if($user){
            $error['msg'] = UserMsg::$reg_already;
            return $error;
        }
        $today = time();
        $now = $today - 60;
        //array('condition' => 'tel=:state', 'params' => array(':state'=>$tel))
        $userCode = UserCode::find()->where(array('tel'=>$tel,  'type'=>1))->andFilterWhere(['>', 'createTime', $now])->orderBy('createTime desc')->asArray()->one();
        if(!$userCode){
            $error['msg'] = UserMsg::$send_error;
            return $error;
        }
        if($userCode['code'] != $code){
            $error['msg'] = UserMsg::$code_error;
            return $error;
        }
        $encrypt = CommonFunction::getRandChar(6);
        $userModel->encrypt = $encrypt;
        $password = CommonFunction::encryptPassword($data['password'], $encrypt);
        $userModel->loginPassword = $password;
        $userModel->phone = $tel;
        $userModel->createTime = $today;
        $userModel->updateTime = $today;
        $session['tel'] = $data['tel'];
        $return = array('status'=>1);
        if(!empty($data['rec']) && $data['rec'] != $data['tel']){
            $rec_tel = CommonFunction::encrypt($data['rec']);
            $rec_info = $userModel->findOne(array('phone'=>$rec_tel));
            if($rec_info){
                $rec_info = $rec_info->attributes;
                $userModel->recID = $rec_info['id'];
                $session['recID'] = $rec_info['id'];
            }
        }
        if(!empty($data['recID'])){
            $userModel->recID = $data['recID'];
            $session['recID'] = $data['recID'];
        }
        $_recID = '';
        if(!empty($data['unionID'])){
            if(!empty($data['recID']))
                $_recID = $data['recID'];
            $return['recID'] = $_recID;
            $userModel->unionID = $data['unionID'];
            if(!empty($data['openID'])){
                $userModel->openID = $data['openID'];
                $session['openID'] = $data['openID'];
            }
            $session['unionID'] = $data['unionID'];
        }
        if($app){
            $key = md5($tel.CommonVar::$encrypt.$password);
            $userModel->appKey = $key;
            $return['appKey'] = $key;
        }
		//微信信息
		if(Http::session('avatar'))
		{
			//头像
			$userModel->avatar = Http::session('avatar')['headimgurl'];
			/*//国家
			$userModel->country = Http::session('avatar')['country'];
			//省份
			$userModel->province = Http::session('avatar')['province'];
			//城市
			$userModel->city = Http::session('avatar')['city'];
			//性别
			$userModel->sex = Http::session('avatar')['sex'];*/
			$session['head'] = Http::session('avatar')['headimgurl'];
		}
        $region_info = RegionService::get($data['tel']);
        $userModel->city = $region_info['c'];
        $userModel->province = $region_info['p'];
        $result = $userModel->save();
        if($result){
            if(isset($data['unionID']))
                $return['userID'] = $userModel->id;
            $session['isNew'] = 1;
            $session['userID'] = $userModel->id;
            if(!$app)
                Http::session('user', $session);
            return $return;
        }
        else{
            return $error;
        }

    }

    public static function hongbao_bind($data){
        $userModel = new User();
        $code = $data['code'];
        $tel = CommonFunction::encrypt($data['tel']);
        $error['status'] = 0;
        $error['msg'] = UserMsg::$time_out;
        $today = time();
        $now = $today - 60;
        //array('condition' => 'tel=:state', 'params' => array(':state'=>$tel))
        $userCode = UserCode::find()->where(array('tel'=>$tel,  'type'=>3))->andFilterWhere(['>', 'createTime', $now])->orderBy('createTime desc')->asArray()->one();
        if(!$userCode){
            $error['msg'] = UserMsg::$send_error;
            return $error;
        }
        if($userCode['code'] != $code){
            $error['msg'] = UserMsg::$code_error;
            return $error;
        }
        $encrypt = CommonFunction::getRandChar(6);
        $userModel->encrypt = $encrypt;
        $password = CommonFunction::encryptPassword(CommonFunction::getRandChar(8), $encrypt);
        $userModel->loginPassword = $password;
        $userModel->phone = $tel;
        $userModel->createTime = $today;
        $userModel->updateTime = $today;
        $session['tel'] = $data['tel'];
        $return = array('status'=>1);
        if(!empty($data['rec']) && $data['rec'] != $data['tel']){
            $rec_tel = CommonFunction::encrypt($data['rec']);
            $rec_info = $userModel->findOne(array('phone'=>$rec_tel));
            if($rec_info){
                $rec_info = $rec_info->attributes;
                $userModel->recID = $rec_info['id'];
                $session['recID'] = $rec_info['id'];
            }
        }
        if(!empty($data['recID'])){
            $userModel->recID = $data['recID'];
            $session['recID'] = $data['recID'];
        }
        $_recID = '';
        if(!empty($data['unionID'])){
            if(!empty($data['recID']))
                $_recID = $data['recID'];
            $return['recID'] = $_recID;
            $userModel->unionID = $data['unionID'];
            if(!empty($data['openID'])){
                $userModel->openID = $data['openID'];
                $session['openID'] = $data['openID'];
            }
            $session['unionID'] = $data['unionID'];
        }
        $region_info = RegionService::get($data['tel']);
        $userModel->city = $region_info['c'];
        $userModel->province = $region_info['p'];
        $result = $userModel->save();
        if($result){
            $session['isNew'] = 1;
            $session['userID'] = $userModel->id;
            $return['userInfo'] = $session;
            return $return;
        }
        else{
            return $error;
        }

    }
}